package com.mobi.cacti.rabbit.controllers;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.holoeverywhere.app.AlertDialog;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.mobi.cacti.LoginActivity;
import com.mobi.cacti.rabbit.connectors.*;
import com.mobi.cacti.rabbit.response.Cache;
import com.mobi.cacti.rabbit.response.CustomerInfoCurcitData;
import com.mobi.cacti.rabbit.response.GraphCircuit;
import com.mobi.cacti.rabbit.response.GraphType;
import com.mobi.cacti.rabbit.response.ReqMobileLogin;
import com.mobi.cacti.rabbit.response.ReqTicketCount;
import com.mobi.cacti.rabbit.response.UserInfo;
import com.mobi.cacti.rabbit.utils.SAXXMLParser;

public class LoginActivityController extends AbstractActivityViewController implements ServerConnectorCallback {
	
	private LoginActivityServerConnector loginServerConnector;
	
	public LoginActivityControllerCallback delegate;
	private static LoginActivityController instance = null;

	public static LoginActivityController getInstance() {
		if(instance == null) {
			instance = new LoginActivityController();
		}
		return instance;
	}
	
	public boolean authenticateWithServer(String username, String password)
	{

		if(!Cache.isNetworkAvailable((Context) delegate))
		{
			Cache.showNoInternetAlertDialog((Context) delegate);
			return false;
		}

		
		//Temporary set username and password
		UserInfo user = new UserInfo();
		user.UserName = username;
		user.Password = password;
		Cache.userInfo = user;
		
		loginServerConnector = new LoginActivityServerConnector();
		loginServerConnector.delegate = this;
		loginServerConnector.authenticateWithServer(username, password);
		
		return true;
	}

	@Override
	public void serverConnectorFinishBackgroundTask(AbstractServerConnector connector, Object response) {
		
		Log.i("tai", "HTTPRESPONSE = "+response);

		if(connector instanceof LoginActivityServerConnector)
		{
			InputStream is = new ByteArrayInputStream(((String)response).getBytes());
			ReqMobileLogin obj = SAXXMLParser.reqMobileLogin(is);
			Cache.reqMobileLogin = obj;

//			Log.i("tai", "reqMobileLogin = "+Cache.reqMobileLogin.Code_Response);
//			Log.i("tai", "CONTACT NUMBER = "+Cache.reqMobileLogin.ContactNumber);
			
			
//			if(((GraphCircuit) Cache.reqMobileLogin.responseCircuitIDWhenLogin.values().toArray()[0]).isDomesticGraphExist())
//				Log.i("tai", "Domestic Graph" + ((GraphCircuit)Cache.reqMobileLogin.responseCircuitIDWhenLogin.values().toArray()[0]).getDomesticGraph(GraphType.DAILY));
//			
//			if(((GraphCircuit) Cache.reqMobileLogin.responseCircuitIDWhenLogin.values().toArray()[0]).isInterGraphExist())	
//				Log.i("tai", "Inter Graph" + ((GraphCircuit)Cache.reqMobileLogin.responseCircuitIDWhenLogin.values().toArray()[0]).getInterGraph(GraphType.WEEKLY));
//			
//			if(((GraphCircuit) Cache.reqMobileLogin.responseCircuitIDWhenLogin.values().toArray()[0]).isTotalGraphExist())
//				Log.i("tai", "Total Graph" + ((GraphCircuit)Cache.reqMobileLogin.responseCircuitIDWhenLogin.values().toArray()[0]).getTotalGraph(GraphType.YEARLY));
			
			this.delegate.loginActivityControllerCompleteAuthenticationToServer();
		}
		
	}
}
